package com.edu.study.base.stuProductItemTemplate.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.edu.study.base.stuProductItemTemplate.mapper.StuProductItemTemplateMapper;
import com.edu.study.base.stuProductItemTemplate.domain.StuProductItemTemplate;
import com.edu.study.base.stuProductItemTemplate.service.IStuProductItemTemplateService;

/**
 * 产品组件库Service业务层处理
 * 
 * @author zw
 * @date 2021-05-11
 */
@Service
//@DataSource(value = DataSourceType.SLAVE)
public class StuProductItemTemplateServiceImpl implements IStuProductItemTemplateService 
{
    @Autowired
    private StuProductItemTemplateMapper stuProductItemTemplateMapper;

    /**
     * 查询产品组件库
     * 
     * @param id 产品组件库ID
     * @return 产品组件库
     */
    @Override
    public StuProductItemTemplate selectStuProductItemTemplateById(String id)
    {
        return stuProductItemTemplateMapper.selectStuProductItemTemplateById(id);
    }

    @Override
    public StuProductItemTemplate getStuProductItemTemplateById(String id) {
        return stuProductItemTemplateMapper.getStuProductItemTemplateById(id);
    }

    /**
     * 查询产品组件库列表
     * 
     * @param stuProductItemTemplate 产品组件库
     * @return 产品组件库
     */
    @Override
    public List<StuProductItemTemplate> selectStuProductItemTemplateList(StuProductItemTemplate stuProductItemTemplate)
    {
        return stuProductItemTemplateMapper.selectStuProductItemTemplateList(stuProductItemTemplate);
    }

    /**
     * 新增产品组件库
     * 
     * @param stuProductItemTemplate 产品组件库
     * @return 结果
     */
    @Override
    public int insertStuProductItemTemplate(StuProductItemTemplate stuProductItemTemplate)
    {
        return stuProductItemTemplateMapper.insertStuProductItemTemplate(stuProductItemTemplate);
    }

    /**
     * 修改产品组件库
     * 
     * @param stuProductItemTemplate 产品组件库
     * @return 结果
     */
    @Override
    public int updateStuProductItemTemplate(StuProductItemTemplate stuProductItemTemplate)
    {
        return stuProductItemTemplateMapper.updateStuProductItemTemplate(stuProductItemTemplate);
    }

    /**
     * 批量删除产品组件库
     * 
     * @param ids 需要删除的产品组件库ID
     * @return 结果
     */
    @Override
    public int deleteStuProductItemTemplateByIds(String[] ids)
    {
        return stuProductItemTemplateMapper.deleteStuProductItemTemplateByIds(ids);
    }

    /**
     * 删除产品组件库信息
     * 
     * @param id 产品组件库ID
     * @return 结果
     */
    @Override
    public int deleteStuProductItemTemplateById(String id)
    {
        return stuProductItemTemplateMapper.deleteStuProductItemTemplateById(id);
    }

    @Override
    public List<Map> buildTreeSelect(List<StuProductItemTemplate> stuProductItemTemplateList) {
        List<Map> treeList = new ArrayList<>();
        for (StuProductItemTemplate stuProductItemTemplate:stuProductItemTemplateList) {
            Map map = new HashMap();
            String id = stuProductItemTemplate.getId();
            map.put("id",id);
            map.put("label",stuProductItemTemplate.getItemName());
            map.put("children",getChildrenList(id));
            treeList.add(map);
        }

        return treeList;
    }

    public List<Map> getChildrenList(String pid){
        List<Map> treeList = new ArrayList<>();
        StuProductItemTemplate stuProductItemTemplate = new StuProductItemTemplate();
        stuProductItemTemplate.setParentId(pid);
        List<StuProductItemTemplate> stuProductItemTemplateList = stuProductItemTemplateMapper.selectStuProductItemTemplateList(stuProductItemTemplate);
        for (StuProductItemTemplate item:stuProductItemTemplateList) {
            Map map = new HashMap();
            String id = item.getId();
            map.put("id",id);
            map.put("label",item.getItemName());
            map.put("children",getChildrenList(id));
            treeList.add(map);
        }
        return treeList;
    }
}
